package cn.jeeweb.web.modules.cable.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.annotation.PageableDefaults;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.cable.entity.CableDetail;
import cn.jeeweb.web.modules.cable.service.ICableDetailService;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @description:电缆明细
 */

@RestController
@RequestMapping("/cable/cableDetail")
@ViewPrefix("modules/SysConfig")
@Log(title = "cable_detail")
public class CableDetailController extends BaseBeanController<CableDetail> {

    @Autowired
    private ICableDetailService cableDetailService;


    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "ajaxList", method = {RequestMethod.GET, RequestMethod.POST})
    @PageableDefaults(sort = "id=desc")
    @Log(logType = ModuleActionType.SELECT)
    public void ajaxList(PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, HttpServletResponse response) throws IOException {
        EntityWrapper<CableDetail> entityWrapper = new EntityWrapper<>(entityClass);
        String cableType = request.getParameter("cableType");
        if (!StringUtils.isEmpty(cableType)) {
            entityWrapper.like("cableType", cableType);
        }
        Page pageBean = cableDetailService.selectPage(PageRequest.getPage(), entityWrapper);
        FastJsonUtils.print(pageBean);
    }

    @PostMapping("add")
    @Log(logType = ModuleActionType.INSERT)
    public Response add(CableDetail entity, BindingResult result,
                        HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        cableDetailService.insert(entity);
        return Response.ok("添加成功");
    }


    @PostMapping("{id}/update")
    @Log(logType = ModuleActionType.UPDATE)
    public Response update(CableDetail entity, BindingResult result,
                           HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        cableDetailService.insertOrUpdate(entity);
        return Response.ok("更新成功");
    }

    @PostMapping("{id}/delete")
    @Log(logType = ModuleActionType.DELETE)
    public Response delete(@PathVariable("id") String id) {
        cableDetailService.deleteById(id);
        return Response.ok("删除成功");
    }

    @PostMapping("batch/delete")
    @Log(logType = ModuleActionType.DELETE)
    public Response batchDelete(@RequestParam("ids") String[] ids) {
        List<String> idList = java.util.Arrays.asList(ids);
        cableDetailService.deleteBatchIds(idList);
        return Response.ok("删除成功");
    }

    @RequestMapping(value = "getCableDetailList", method = {RequestMethod.GET, RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public void getCableDetailList() {
        EntityWrapper<CableDetail> entityWrapper = new EntityWrapper<>(CableDetail.class);
        List<CableDetail> cableDetails = cableDetailService.selectList(entityWrapper);
        FastJsonUtils.print(cableDetails);
    }

}